<script>
	import {
		login,
		getUserInfo
	} from './api/api.js'

	export default {
		onLaunch() {
			// this.login()
			this.websocket()
		},
		globalData: {
			cartData: {}, //购物车数据
			clear: false,
			tableNum: '', //桌号
			webs: {}, //websocket
			userInfo: {}, //用户信息
			encryptedData: '',
			iv: '',
		},
		methods: {
			getUserProfile() {
				return new Promise((resolve, reject) => {
					wx.getUserProfile({
						desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: async (res) => {
							this.userInfo = res.userInfo
							this.encryptedData = res.encryptedData
							this.iv = res.iv
							await this.login()
							resolve()
						}
					})
				})
			},
			async login() {
				const data = await uni.login({
					provider: 'weixin'
				})
				const res = await login({
					js_code: data[1].code,
					encryptedData: this.encryptedData,
					iv: this.iv,
					userInfo: this.userInfo
				})
				uni.setStorageSync('token', res.data.token)
				this.getUserInfo()
			},
			websocket() {
				uni.connectSocket({
					url: 'ws://localhost:5000/common/orderRemind?id=111111'
				})
				uni.onSocketOpen(function(res) {
					console.log('WebSocket连接已打开！')
				})
				uni.onSocketClose(function(res) {
					console.log('WebSocket 已关闭！')
				})
			},
			async getUserInfo() {
				const res = await getUserInfo()
				getApp().globalData.userInfo = res.data
				return getApp().globalData.userInfo
			}
		},
		// onShow: function() {
		// 	console.log('App Show')
		// },
		// onHide: function() {
		// 	console.log('App Hide')
		// }
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "./static/css/iconfont.css";
	@import "uview-ui/index.scss";
	@import "colorui/main.css";
	@import "colorui/icon.css";
</style>
